# 打印元组二叉树

import json
import os

from pyecharts import options as opts
from pyecharts.charts import Page, Tree

# tup = (((((((((15, 14), (((((32, 19), 29), 5), ( (((((((23, 2), 13), 17), (31, 22)), (33, 24)), ((28, 27), 20)),
# (((4, 0), (10, 3)), 21)), (30, 8))), 26)), 25), 16), 7), (18, 6)), (11, 1)), 9), 12)
#
tup2 = (((('JLCGHJ', 'GZLGCGHJ'), ('YNQJHJ', 'KMHJ')), (((('GZYYHJ', ('GZHQDYYYHJ', 'WJCHJ')), (
    ('SMDYYYHJ', ('WHJ1H', ('GZDYCYLGHJ', 'SWDYYYHJ'))), ('GZHQBLYHJ', ('GZDYYYHJ', 'GZHQYYLSHJ')))), ((('GZCYHJ', (
    ('JLDYHJ', 'SMDYCYHJ'), ('SMZGZYHJ', ('JLXYHJ', ('GZDYCYZGHJ', 'GZZLDYCYHJM'))))), ('JLCYHJ', (
    ('SWDYCYHJ', 'SWDYTHJ'), (('GZHQCYHJ', 'SMZGLYHJ'), ('LYZPZGHJ', 'ZJLSHJ'))))), ('GZZLDYCYHJ', 'SMDTHJ'))),
                                                        ('HNLYHJ', 'HNBDMHJ'))), ('HNHJ', 'GZJTHJ'))


def get_dict_tree(tup):
    """
    :param tup:传入一个元组二叉树，中间结点无数据，叶节点有数据
    :return: 返回标准二叉树字典，用于画图
    """
    if isinstance(tup, tuple):
        dic = {"children": [], "name": ' '}
        dic["children"].append(get_dict_tree(tup[0]))
        dic["children"].append(get_dict_tree(tup[1]))
    else:
        dic = {"children": [], "name": tup}
    return dic


# dic = get_dict_tree(tup)
# data = [
#     dic
# ]
#
# tree = (
#     Tree()
#     .add("", data, orient="TB")
#     .set_global_opts(title_opts=opts.TitleOpts(title="Tree-基本示例"))
# )
#
# tree.render()

def print_tree_tuple(tree_tuple, file_path):
    dic = get_dict_tree(tree_tuple)
    # print(dic)
    data = [
        dic
    ]

    tree = (
        Tree()
        .add("", data)
        .set_global_opts(title_opts=opts.TitleOpts(title="Tree-基本示例"))
    )
    tree.render(path=file_path)


# print_tree_tuple(tup2, 'C:/Users/Administrator/Desktop/tree.html')
